package com.gdth.bank.presenter;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import com.gdth.bank.model.CjkhZcxxModel;
import com.gdth.bank.view.CjkhZcxxView;
import com.gdth.database.bean.TAppBfrjJtcyb;
import com.gdth.database.bean.TAppBfrjZcxxb;

public class CjkhZcxxPresenter {
	private CjkhZcxxView mCjkhZcxxView;
	private CjkhZcxxModel mCjkhZcxxModel;
	
	public CjkhZcxxPresenter(CjkhZcxxView mCjkhZcxxView) {
		this.mCjkhZcxxView = mCjkhZcxxView;
		this.mCjkhZcxxModel = new CjkhZcxxModel();
	}
	
	public void getBfjlByUuid(String jcuuid){
		mCjkhZcxxModel.getZcxxByUuid(jcuuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<TAppBfrjZcxxb>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mCjkhZcxxView.showMessage("获取失败");
			}

			@Override
			public void onNext(List<TAppBfrjZcxxb> arg0) {
				// TODO Auto-generated method stub
				mCjkhZcxxView.getAllBrjl(arg0);
			}
		});
	}

	public void getjtcyxx(String jcuuid){
		mCjkhZcxxModel.getjtcyxxUuid(jcuuid)
				.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Subscriber<List<TAppBfrjJtcyb>>(){

					@Override
					public void onCompleted() {
						// TODO Auto-generated method stub
					}

					@Override
					public void onError(Throwable arg0) {
						// TODO Auto-generated method stub
						mCjkhZcxxView.showMessage("");
					}

					@Override
					public void onNext(List<TAppBfrjJtcyb> arg0) {
						// TODO Auto-generated method stub
						mCjkhZcxxView.getAllJtcy(arg0);
					}

				});
	}
	
	public void save(TAppBfrjZcxxb mTAppBfrjZcxxb, final int type) {
		mCjkhZcxxView.showProgressDialog("正在保存...");
		
		mCjkhZcxxModel.saveCjkhZcxx(mTAppBfrjZcxxb)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<Long>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					mCjkhZcxxView.hideProgressDialog();
				}

				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					mCjkhZcxxView.showMessage(arg0.getMessage());
				}

				@Override
				public void onNext(Long arg0) {
					// TODO Auto-generated method stub
					mCjkhZcxxView.saveSuccess(arg0, type);
				}
				
			});
	}
	
	public void delete(TAppBfrjZcxxb mTAppBfrjZcxxb) {
		mCjkhZcxxView.showProgressDialog("正在保存...");
		final TAppBfrjZcxxb tAppBfrjZcxxb = mTAppBfrjZcxxb;
		mCjkhZcxxModel.delete(tAppBfrjZcxxb)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<Long>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					mCjkhZcxxView.hideProgressDialog();
				}

				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					mCjkhZcxxView.showMessage(arg0.getMessage());
				}

				@Override
				public void onNext(Long arg0) {
					// TODO Auto-generated method stub
					mCjkhZcxxView.deleteSuccess(tAppBfrjZcxxb);
				}
				
			});
	}
}
